package com.mrd.gtimp.base.service;

import com.mrd.base.BaseBiz;
import com.mrd.framework.ui.filter.Filter;
import com.mrd.framework.ui.filter.SearchRule;
import com.mrd.framework.ui.filter.SearchRuleOption;
import com.mrd.gtimp.base.entity.Company;
import com.mrd.gtimp.base.entity.SystemCopInterface;
import com.mrd.gtimp.base.entity.SystemInterface;
import com.mrd.gtimp.base.repository.SystemInterfaceDao;
import com.mrd.spring.repository.BaseRepository;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

/**
 * <p> application name: gtimp <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author qiul
 */
@Service
public class SystemCopInterfaceBiz extends BaseBiz<SystemCopInterface> {

    @Resource
    private SystemInterfaceDao systemInterfaceDao;

	/*
     * (non-Javadoc)
	 * @see com.mrd.gtimp.base.biz.SystemInterfaceBiz#CreateInterfaceByTemplate(java.lang.String,
	 * java.lang.String)
	 */

    public boolean CreateInterfaceByTemplate(String companyId, String type, String user) {
        Filter filters = new Filter();
        if (StringUtils.isNotEmpty(type)) {
            filters.addRule(new SearchRule("systemValue.identifier", SearchRuleOption.cn, type));
        }
        try {// TODO: 2016/8/17
            List<SystemInterface> itfs = null;//systemInterfaceDao.findSystemInterface(companyId, type);
            if (itfs != null && itfs.size() != 0) {
                Company company = new Company();
                company.setId(companyId);
                List<SystemCopInterface> entities = new ArrayList<SystemCopInterface>();
                for (SystemInterface systemInterface : itfs) {
                    SystemCopInterface sci = new SystemCopInterface();
                    sci.setCompany(company);
                    sci.setCreator(user);
                    sci.setModifier(user);
                    sci.setCtime(new Date());
                    sci.setMtime(new Date());
                    sci.setSystemInterface(systemInterface);
                    sci.setType(systemInterface.getSystemValue().getIdentifier());
                    entities.add(sci);
                }
                baseRepository.saveEntities(entities);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    @Resource
    // 该处指定注入BaseRepository的dao对象
    @Qualifier("systemCopInterfaceDao")
    public void setBaseRepository(BaseRepository<SystemCopInterface, String> baseRepository) {
        super.baseRepository = baseRepository;
    }
}
